package com.inno.life.application.mq;


import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.inno.life.common.constant.MessageTypeConstant;
import com.inno.life.common.enums.YesOrNoEnum;
import com.inno.life.domain.entity.PlannerScheduleManagerBO;
import com.inno.life.domain.service.ScheduleManagerDomainService;
import com.inno.life.domain.utils.ApplicationContextUtil;
import com.inno.life.domain.websocket.ZeyPlannerSocket;
import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.inno.life.common.constant.RocketMQConstant.ConsumerGroupConstant.SCHEDULE_MANAGER_REMINDER_CONSUMER;
import static com.inno.life.common.constant.RocketMQConstant.TopicConstant.SCHEDULE_MANAGER_REMIND;

/**
 * @author sean
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = SCHEDULE_MANAGER_REMIND,consumerGroup = SCHEDULE_MANAGER_REMINDER_CONSUMER)
public class ScheduleManagerReminderListener implements RocketMQListener<MessageExt> {

    private final ScheduleManagerDomainService scheduleManagerDomainService;

    @Autowired
    public ScheduleManagerReminderListener(ScheduleManagerDomainService scheduleManagerDomainService){
        this.scheduleManagerDomainService = scheduleManagerDomainService;
    }
    @Override
    public void onMessage(MessageExt messageExt) {
        // 获取到messageExt的PlannerScheduleManagerBO
        byte[] body = messageExt.getBody();
        String messageBody = new String(body);
        PlannerScheduleManagerBO plannerScheduleManagerBO = JSON.parseObject(messageBody, PlannerScheduleManagerBO.class);
        log.info("解析后的消息：{}", plannerScheduleManagerBO);
        String remindMode = plannerScheduleManagerBO.getRemindMode();
        switch (remindMode){
            case "0":
                // 无需提醒
                break;
            case "1":
                // 短信提醒
                break;
            case "2":
                // 邮件提醒
                break;
            case "3":
                // 软件内部消息提醒
                sendWebsocketMessage(plannerScheduleManagerBO);
                break;
            default:
                // 默认为0，无需提醒
                break;
        }

    }

    /**
     * 发送websocket消息提醒
     */
    public void sendWebsocketMessage(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        Long userId = plannerScheduleManagerBO.getUserId();
        ZeyPlannerSocket socket = ApplicationContextUtil.getWebSocket();
        Session session = socket.getSession(userId);
        if (session != null && session.isOpen()) {
            String topic = plannerScheduleManagerBO.getTopic();
            String content = plannerScheduleManagerBO.getContent();
            JSONObject message = new JSONObject();
            message.putOnce("title", "日程提醒");
            message.putOnce("type", MessageTypeConstant.SCHEDULE_MANAGER_REMIND);
            message.putOnce("message", "您今日有一个日程待完成，请注意时间安排: " + topic + ": " + content);
            socket.sendMessage(session, message.toString());
            plannerScheduleManagerBO.setRemindComplete(YesOrNoEnum.YES.getCode());
            plannerScheduleManagerBO.setIsComplete(YesOrNoEnum.YES.getCode());
            this.scheduleManagerDomainService.edit(plannerScheduleManagerBO);
        } else {
            log.warn("用户{}的socket连接已断开或不存在，无法发送提醒消息", userId);
        }
    }

}
